<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

/**
* This controller is used for JAConfiguration feature of the component
*/
class JAJobBoardControllerJAConfiguration extends JAJobBoardController{

    /**
     * Constructor
     */
    function __construct( $jbconfig = array() )
    {
        parent::__construct( $jbconfig );    
        checkPermission_Administrator('configuration');    
    }
    
    
    /**
    * Display current JAConfiguration of the component to administrator
    * 
    */
    function display(){
		parent::display();
    }            
		
	/**
	* Save configuration record
	*/
	function save(){
		global $option;
		
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cache = & JFactory::getCache($option);
		$cache->clean();
		
		$tab = JRequest::getVar('tab', 'general');
		if($tab=='') $tab = 'general';
		
		$paramsField = JRequest::getVar( $tab, null, 'post', 'array' );
		if ($tab=='general')
		{
			$allowed_extend = JobBoardConstant::getAllowedExtend();
			//Check extend file
			$temp = strtolower($paramsField['allowed_ext']);
			$temp = explode(',',$temp);
			for ($i=0;$i<count($temp);$i++)
				$temp[$i] = trim($temp[$i]);
			$diff = array_diff($temp, $allowed_extend);
			if ($diff)
			{
				JError::raiseWarning(4,JText::_('Updating failed! You have fill some restricted extend file').': <br />&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp '.implode(', ',$diff));
				JRequest::setVar('save_error',1);
				$this->display();
				
				return;
			}
			//Check extend image
			$temp = explode(',',$paramsField['allowed_img']);
			for ($i=0;$i<count($temp);$i++)
				$temp[$i] = trim($temp[$i]);
			$diff = array_diff($temp, $allowed_extend);
			if ($diff)
			{
				JError::raiseWarning(4,JText::_('Updating failed! You have fill some restricted extend image').': <br />&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp '.implode(', ',$diff));
				JRequest::setVar('save_error',1);
				$this->display();
				return;
			}
		}
		
		$apply_option = JRequest::getVar('apply_option');
		if ($apply_option)
		{
			$apply_option = implode(',',$apply_option);
			$paramsField['apply_option'] = $apply_option;
		}
		// Build parameter INI string
		
		if ($paramsField)
		{	
			$model	=& $this->getModel('jaconfiguration');	
			
			$params = new JParameter('');
			foreach ($paramsField as $k => $v){
				$params->set($k, $v);					
			}					
			$post['data'] = $params->toString();
			
			$model->setState( 'request', $post );	
			if ($id = $model->store()) {
				$msg = JText::_( 'Successfully updated configuration' );
			} else {
				$msg = JText::_( 'Error found!Fail to update configuration' );
			}	
		}
		
		$this->setRedirect("index.php?option=$option&view=jaconfiguration&tab=$tab", $msg);		
	}
	
	function cleancache(){
	    global $jbconfig;
	    
	    $msg = '';
	    
	    jimport('joomla.filesystem.folder');
	    if(isset($jbconfig['cache']) && JFolder::exists($jbconfig['cache']->get('cache_base'))){
	       if(JFolder::delete($jbconfig['cache']->get('cache_base'))){
	           $msg = JText::_('Successfully cleaned cache.');
	       }
	       else {
	           $msg = JText::_('Clean unsuccessfull.');
	       }
	    }
	    else{
	         $msg = JText::_('Cache folder not found.');
	    }
	    
	    $this->setRedirect('index.php?option=com_jajobboard&view=jaconfiguration&tab=cache', $msg);
	}
}
?>